package com.znyj.servlet.sqlHelper;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by nameless on 15-2-13.
 */
public class SqlRequest {

    private String targetFields;
    private String table;
    private List<Where> wheres;
    private List<Order> orders;
    private Limit limit;
    private boolean isDistinct;

    public SqlRequest() {
        this.isDistinct = false;
    }

    public SqlRequest(String table, String targetFields) {
        this.targetFields = targetFields;
        this.table = table;
        this.isDistinct = false;
    }

    public String getTargetFields() {
        return targetFields;
    }

    public void setTargetFields(String targetFields) {
        this.targetFields = targetFields;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public List<Where> getWheres() {
        return wheres;
    }

    public void setWheres(List<Where> wheres) {
        this.wheres = wheres;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public Limit getLimit() {
        return limit;
    }

    public void setLimit(Limit limit) {
        this.limit = limit;
    }

    public boolean getIsDistinct() {
        return isDistinct;
    }

    public void setIsDistinct(boolean isDistinct) {
        this.isDistinct = isDistinct;
    }



    public void addWhere(Where where) {
        if (this.wheres == null) {
            wheres = new ArrayList<>();
        }
        wheres.add(where);
    }

    public void addOrder(Order order) {
        if (this.orders == null) {
            orders = new ArrayList<>();
        }
        orders.add(order);
    }

    public void addOrder(String fieldName, Order.Sort sort) {
        Order order = new Order(fieldName, sort);
        this.addOrder(order);
    }

    public SqlRequest EQ(String fieldName, Object value) {
        Where where = new Where(Where.Judge.EQ, fieldName, value);
        this.addWhere(where);
        return this;
    }

    public SqlRequest NOT_EQ(String fieldName, Object value) {
        Where where = new Where(Where.Judge.NOT_EQ, fieldName, value);
        this.addWhere(where);
        return this;
    }

    public SqlRequest LT(String fieldName, Object value) {
        Where where = new Where(Where.Judge.LT, fieldName, value);
        this.addWhere(where);
        return this;
    }

    public SqlRequest LE(String fieldName, Object value) {
        Where where = new Where(Where.Judge.LE, fieldName, value);
        this.addWhere(where);
        return this;
    }

    public SqlRequest BT(String fieldName, Object value) {
        Where where = new Where(Where.Judge.BT, fieldName, value);
        this.addWhere(where);
        return this;
    }

    public SqlRequest BE(String fieldName, Object value) {
        Where where = new Where(Where.Judge.BE, fieldName, value);
        this.addWhere(where);
        return this;
    }

    public SqlRequest LIKE(String fieldName, Object value) {
        Where where = new Where(Where.Judge.LIKE, fieldName, value);
        this.addWhere(where);
        return this;
    }

    public SqlRequest IN(String fieldName, Object value) {
        Where where = new Where(Where.Judge.IN, fieldName, value);
        this.addWhere(where);
        return this;
    }

    public SqlRequest CAST(String fieldName, Object value) {
        Where where = new Where(Where.Judge.CAST, fieldName, value);
        this.addWhere(where);
        return this;
    }



    public SqlRequest Limit(int skip, int number) {
        if (skip == 0) {
            return Limit(number);
        }
        Limit methodLimit = new Limit(skip, number);
        this.setLimit(methodLimit);
        return this;
    }

    public SqlRequest Limit(int number) {
        Limit methodLimit = new Limit(number);
        this.setLimit(methodLimit);
        return this;
    }

    public SqlRequest Sort(String fieldName, Order.Sort sort) {
        Order order = new Order(fieldName, sort);
        this.addOrder(order);
        return this;
    }

    public String makeSql() {
        StringBuilder builder = new StringBuilder();
        builder.append("SELECT ");
        if(isDistinct)
        {
            builder.append(" DISTINCT ");
        }
        builder.append(targetFields);
        builder.append(" FROM ");
        builder.append(table);
        if (!CollectionUtils.isEmpty(wheres)) {
            builder.append(SqlBuildHelper.makeWheres(wheres));
        }
        if (!CollectionUtils.isEmpty(orders)) {
            builder.append(SqlBuildHelper.makeOrders(orders));
        }
        if (null != limit) {
            builder.append(SqlBuildHelper.makeLimit(limit));
        }

        return builder.toString();
    }
}
